#ifndef FLASH_25Q64_H
#define FLASH_25Q64_H

#include "rtthread.h"

/* HG25064 指令定义 */
#define HG25Q64_CMD_WRITE_ENABLE          0x06
#define HG25Q64_CMD_WRITE_DISABLE         0x04
#define HG25Q64_CMD_READ_STATUS_REG1      0x05
#define HG25Q64_CMD_READ_STATUS_REG2      0x35
#define HG25Q64_CMD_READ_STATUS_REG3      0x15
#define HG25Q64_CMD_WRITE_STATUS_REG1     0x01
#define HG25Q64_CMD_WRITE_STATUS_REG2     0x31
#define HG25Q64_CMD_WRITE_STATUS_REG3     0x11
#define HG25Q64_CMD_READ_DATA             0x03
#define HG25Q64_CMD_FAST_READ             0x0B
#define HG25Q64_CMD_PAGE_PROGRAM          0x02
#define HG25Q64_CMD_SECTOR_ERASE_4K       0x20
#define HG25Q64_CMD_BLOCK_ERASE_32K       0x52
#define HG25Q64_CMD_BLOCK_ERASE_64K       0xD8
#define HG25Q64_CMD_CHIP_ERASE            0xC7
#define HG25Q64_CMD_POWER_DOWN            0xB9
#define HG25Q64_CMD_RELEASE_POWER_DOWN    0xAB
#define HG25Q64_CMD_READ_MANUFACTURER_ID  0x90
#define HG25Q64_CMD_READ_JEDEC_ID         0x9F
#define HG25Q64_CMD_ENABLE_RESET          0x66
#define HG25Q64_CMD_RESET_DEVICE          0x99

/* HG25064 参数定义 */
#define HG25Q64_MANUFACTURER_ID           0xEF  /* 华冠半导体制造商ID */
#define HG25Q64_DEVICE_ID                 0x16  /* 设备ID */
#define HG25Q64_PAGE_SIZE                 256   /* 页大小 */
#define HG25Q64_SECTOR_SIZE               4096  /* 扇区大小 */
#define HG25Q64_BLOCK_SIZE_32K            32768 /* 32K块大小 */
#define HG25Q64_BLOCK_SIZE_64K            65536 /* 64K块大小 */
#define HG25Q64_TOTAL_SIZE                (8 * 1024 * 1024) /* 总容量 8MB */

/* 状态寄存器位定义 */
#define HG25Q64_STATUS_BUSY               0x01
#define HG25Q64_STATUS_WEL                0x02


// Flash 存储管理器
typedef struct {
    rt_uint8_t name[25];             
    rt_uint8_t *buf;    // 数据数组
    rt_uint32_t size;     // 总大小
    uint32_t current_addr;   // 当前写入地址（扇区编号0-16）
    rt_bool_t dirty_flag;
} flash_storage_t;

extern flash_storage_t flash_dat[];

rt_uint16_t flash_25q64_readID(void);
void flash_25q64_wait_busy(void);
void flash_25q64_sector(uint32_t addr);
rt_int16_t flash_25q64_write(rt_uint8_t *buffer, rt_uint32_t addr, rt_uint16_t numbyte);
void flash_25q64_read(rt_uint8_t *buffer, rt_uint32_t addr, rt_uint16_t numbyte);


#endif
